import os
from pathlib import Path

testcase_dir = "E:/project_test/testcases"
if not os.path.exists(testcase_dir):
    os.makedirs(testcase_dir)

yaml_dir = "E:/project_test/data"
yaml_files = Path(yaml_dir).glob("*.yaml")

for yaml_file in yaml_files:
    # 获取文件名及路径，并将路径转换成UTF-8编码的字符串
    yaml_path = str(yaml_file.as_posix())
    testcase_name = yaml_file.stem  # 文件名作为测试用例的名称
    testcase_code = f"""
# coding=utf-8
import requests
from common.logger_util import setup_logger
from common.yaml_util import YamlUtil
from common.request_util import RequestUtil

def test_{testcase_name}():
    yaml_util = YamlUtil()
    datas = yaml_util.read_testcase('{yaml_path}')
    for data in datas:

        print(data)
        url = data['request']['url']
        method = data['request']['method']
        headers = data['request']['headers']
        payload = data['request']['data']

        response = RequestUtil("base_test_url").send_request(method, url, json=payload, headers=headers)

        validation = data['validate']
        for v in validation:
            if 'equals' in v:
                assert response.status_code == v['equals']['status_code']
            elif 'contains' in v:
                assert v['contains'] in response.text
if __name__ == "__main__":
    test_{testcase_name}()
"""
    with open(os.path.join(testcase_dir, f"test_{testcase_name}.py"), "w", encoding='utf-8') as f:
        f.write(testcase_code)